﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Applee.FractCommon;
using System.Drawing;

namespace Applee.PentagramFract
{
    public class PentagramFract : BaseFract
    {
        protected override void Initialize()
        {
            float lctAngle = (float)(18.0F * Math.PI / 180);
            float lcbAngle = (float)(36.0F * Math.PI / 180);

            SizeF shapeSize = new SizeF(2 * (1 + (float)Math.Sin(lctAngle)),
                (1 + (float)Math.Cos(lcbAngle)) * (float)(Math.Cos(lctAngle) + Math.Sin(lctAngle) / Math.Tan(lcbAngle)));

            float flength = 0;
            if (MathUtils.CalcAdaptiveLength(mViewSize, shapeSize, ref flength))
            {
                //width
                mLength = flength / ((float)(2 * (1 + (float)Math.Sin(lctAngle))));
            }
            else
            {
                //Height
                mLength = flength / (float)((1 + (float)Math.Cos(lcbAngle)) * (float)(Math.Cos(lctAngle) + Math.Sin(lctAngle) / Math.Tan(lcbAngle)));
            }

            mLength -= 10;
        }

        protected void CreateFract2(System.Drawing.PointF pointCenter, PointF pointOnEdge, RandomColor color, float fLength, int iterations)
        {
            if (iterations < 0)
                return;

            Pentagram p = new Pentagram();
            p.MakeShape(pointCenter, pointOnEdge, fLength);
            p.Color = color.GetColor(iterations);
            mFractObjects.Add(p);

            float lctAngle = (float)(18.0F * Math.PI / 180);
            float lcbAngle = (float)(36.0F * Math.PI / 180);

            //R = L (cos18 + sin18 / tan36)
            float fRad = fLength * ((float)Math.Cos(lctAngle) + (float)Math.Sin(lctAngle) / (float)Math.Tan(lcbAngle));

            PointF pt1 = MathUtils.ExtendPoint(pointCenter, p.Points[0], fRad * 0.34F);
            CreateFract2(pt1, p.Points[0], color, fLength * 0.34F, iterations - 1);

            PointF pt2 = MathUtils.ExtendPoint(pointCenter, p.Points[2], fRad * 0.34F);
            CreateFract2(pt2, p.Points[2], color, fLength * 0.34F, iterations - 1);

            PointF pt3 = MathUtils.ExtendPoint(pointCenter, p.Points[4], fRad * 0.34F);
            CreateFract2(pt3, p.Points[4], color, fLength * 0.34F, iterations - 1);

            PointF pt4 = MathUtils.ExtendPoint(pointCenter, p.Points[6], fRad * 0.34F);
            CreateFract2(pt4, p.Points[6], color, fLength * 0.34F, iterations - 1);

            PointF pt5 = MathUtils.ExtendPoint(pointCenter, p.Points[8], fRad * 0.34F);
            CreateFract2(pt5, p.Points[8], color, fLength * 0.34F, iterations - 1);

        }

        protected override void CreateFract(System.Drawing.PointF point, RandomColor color, float fLength, int iterations)
        {
            if (iterations < 0)
                return;

            Pentagram p = new Pentagram();
            p.MakeShape(point, fLength);
            p.Color = color.GetColor(iterations);
            mFractObjects.Add(p);

            float lctAngle = (float)(18.0F * Math.PI / 180);
            float lcbAngle = (float)(36.0F * Math.PI / 180);

            //R = L (cos18 + sin18 / tan36)
            float fRad = fLength * ((float)Math.Cos(lctAngle) + (float)Math.Sin(lctAngle) / (float)Math.Tan(lcbAngle));

            PointF pt1 = MathUtils.ExtendPoint(point, p.Points[0], fRad * 0.34F);
            CreateFract2(pt1, p.Points[0], color, fLength * 0.34F, iterations - 1);

            PointF pt2 = MathUtils.ExtendPoint(point, p.Points[2], fRad * 0.34F);
            CreateFract2(pt2, p.Points[2], color, fLength * 0.34F, iterations - 1);

            PointF pt3 = MathUtils.ExtendPoint(point, p.Points[4], fRad * 0.34F);
            CreateFract2(pt3, p.Points[4], color, fLength * 0.34F, iterations - 1);

            PointF pt4 = MathUtils.ExtendPoint(point, p.Points[6], fRad * 0.34F);
            CreateFract2(pt4, p.Points[6], color, fLength * 0.34F, iterations - 1);

            PointF pt5 = MathUtils.ExtendPoint(point, p.Points[8], fRad * 0.34F);
            CreateFract2(pt5, p.Points[8], color, fLength * 0.34F, iterations - 1);

        }
    }
}
